//
//  TabBarButton.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/6.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

class TabBarButton: UIButton {

    let ImageRidio : CGFloat = 0.7
    //self.adjustsImageWhenHighlighted = false
    lazy var badgeView : BadgeView = {
        let view  = BadgeView(type: UIButtonType.Custom)
        self.addSubview(view)
        return view
    }()
    
    var item : UITabBarItem!{
        didSet{
            setItem()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Selected)
        self.adjustsImageWhenHighlighted = false
        
        // 图片居中
        self.imageView?.contentMode = UIViewContentMode.Center
        // 文字居中
        self.titleLabel?.textAlignment = NSTextAlignment.Center
        // 设置文字字体
        self.titleLabel?.font = UIFont.systemFontOfSize(12)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setItem(){
        self.observeValueForKeyPath(nil, ofObject: nil, change: nil, context: nil)
        item.addObserver(self, forKeyPath: "title", options: NSKeyValueObservingOptions.New, context: nil)
        item.addObserver(self, forKeyPath: "image", options: NSKeyValueObservingOptions.New, context: nil)
        item.addObserver(self, forKeyPath: "selectedImage", options: NSKeyValueObservingOptions.New, context: nil)
        item.addObserver(self, forKeyPath: "badgeValue", options: NSKeyValueObservingOptions.New, context: nil)
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        self.setTitle(item.title, forState: UIControlState.Normal)
        self.setImage(item.image, forState: UIControlState.Normal)
        self.setImage(item.selectedImage, forState: UIControlState.Selected)
        badgeView.badgeValue = item.badgeValue
    }
    
        
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        // 1.imageView
//        CGFloat imageX = 0;
//        CGFloat imageY = 0;
//        CGFloat imageW = self.bounds.size.width;
//        CGFloat imageH = self.bounds.size.height * CZImageRidio;
//        self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
//        
//        
//        // 2.title
//        CGFloat titleX = 0;
//        CGFloat titleY = imageH - 3;
//        CGFloat titleW = self.bounds.size.width;
//        CGFloat titleH = self.bounds.size.height - titleY;
//        self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
//        
//        // 3.badgeView
//        self.badgeView.x = self.width - self.badgeView.width - 10;
//        self.badgeView.y = 0;
        
        let imageX : CGFloat = 0
        let imageY : CGFloat = 0
        let imageW = self.bounds.size.width
        let imageH = self.bounds.size.height * ImageRidio
        self.imageView?.frame = CGRectMake(imageX, imageY, imageW, imageH)
        
        let titleX : CGFloat = 0
        let titleY : CGFloat = imageH - 3
        let titleW = self.bounds.size.width
        let titleH = self.bounds.size.height - titleY
        self.titleLabel?.frame = CGRectMake(titleX, titleY, titleW, titleH)
        
        self.badgeView.x = self.width - self.badgeView.width - 10
        self.badgeView.y = 0
    }
}
